package com.powernode.controller;


import com.github.pagehelper.Page;
import com.powernode.common.Constants;
import com.powernode.domain.Result;
import com.powernode.domain.User;
import com.powernode.dto.UserDto;
import com.powernode.service.UserService;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("query")
public class QueryController {

    @Resource
    private UserService userService;

    @GetMapping("queryUser.action")
    public Result queryUser(UserDto userDto){

        Page<User> pageUser = userService.queryPage(userDto);
        return new Result(0,"查询成功",pageUser.getTotal(),pageUser.getResult());
    }

    @PostMapping("add.action")
    public Result addUser(User user){
        user.setPassword(DigestUtils.md5DigestAsHex(Constants.USER_PASSWORD.getBytes()));
        user.setDeleted(Constants.USER_NUM_1);
        user.setImg(Constants.USER_IMG);
        user.setCreateTime(new Date());

        try {
            userService.insertSelective(user);
            return new Result(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }
    @PostMapping("update.action")
    public Result updateUser(User user){

        try {
            userService.updateByPrimaryKeySelective(user);
            return new Result(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }
    @PostMapping("delete.action")
    public Result deleteUser(User user){
            user.setDeleted(Constants.USER_NUM_2);
            user.setDeletedTime(new Date());
        try {
            userService.updateByPrimaryKeySelective(user);
            return new Result(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }
    @PostMapping("resetPwd.action")
    public Result resetPwd(User user){
        user.setPassword(DigestUtils.md5DigestAsHex(Constants.USER_PASSWORD.getBytes()));
        try {
            userService.updateByPrimaryKeySelective(user);
            return new Result(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }

    @GetMapping("toALLUser.action")
    public Result toALLUser(){
        Integer role = Constants.USER_COLE_NORMAL;
        Integer deleted = Constants.USER_NUM_1;
        List<User> userList = userService.getAllUser(role,deleted);
        return new Result(200,"查询成功",0L,userList);
    }

    /**
     * 验证密码
     * @param oldPwd
     * @param session
     * @return
     */
    @PostMapping("checkOldPwd.action")
    public Result checkOldPwd(String oldPwd, HttpSession session){
        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        User user2 = userService.selectByPrimaryKey(user.getId());
        if (DigestUtils.md5DigestAsHex(oldPwd.getBytes()).equals(user2.getPassword())){
            return new Result(200,"验证成功");
        }
        return new Result(0,"验证失败");
    }
    @PostMapping("changePwd.action")
    public Result changePwd(String newPwd, HttpSession session){
        try {
            User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
            User user2 = userService.selectByPrimaryKey(user.getId());
            user2.setPassword(DigestUtils.md5DigestAsHex(newPwd.getBytes()));
            userService.updateByPrimaryKeySelective(user2);
            return new Result(200,"密码修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"密码修改失败");
        }


    }
}
